<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/1
 * Time: 14:11
 */

namespace app\home\model;


use think\Model;
use think\Db;
/**村规民约前台模型
 * Class regulation
 * @package app\home\model
 */
class Regulation extends Model
{
    /*
    * 获取文章列表
    * */
    public function getList($type,$length = 0){
        $map = [
            'type'=>$type,
            'status'=>0
        ];
        $field = ['id,title,create_time,description,views'];
        $res = $this->where($map)->field($field)->order('create_time desc')->limit($length,12)->select();
        $list = [];
        foreach ($res as $value){
            $value['time'] = date('Y-m-d',$value['create_time']);
            $list[] = $value;
        }
        return $list;
    }

    /*
     * 浏览限制*/
    public function setView($id)
    {
        cookie(['prefix' => "$id", 'expire' => 1]);  //根据id设置不同文章的cook名称
        $view = cookie('view_time');
        if (empty($view)) {                           //文章第一次访问未设置cookie，设置cookie并插入一条访问记录
            cookie('view_time', time());
            $this->where('id', $id)->setInc('views');
        } else {
            if ((time() - $view) > 1) {               //在一小时内重复刷新无浏览量，大于一小时重新设置cookie的值
                cookie('view_time', time());
                $this->where('id', $id)->setInc('views');   //增加访问次数
            }
        }
    }
}